#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
#define db double
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define mdI int mid=(l+r)>>1
#define pbI push_back
#define ll long long
#define MOD 998244353
#define lowbit(x) x&(-x)
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define inf 1e9
void chmi(ll &x,ll y){x=min(x,y);}
void chmx(ll &x,ll y){x=max(x,y);}
ll qread(){
    char o=0;ll f=1,x=0;
    for(;!isdigit(o);o=getchar())if(o=='-')f=-1;
    for(;isdigit(o);o=getchar())x=x*10+(o-'0');
    return f*x;
}
ll qp(ll a,ll b,ll c=MOD){ll rs=1,bs=a;while(b){if(b&1)rs=rs*bs%c;bs=bs*bs%c;b>>=1;}return rs;}
bool FLA;
int n,m,_a[MAXN],c[MAXN],d[20][MAXN],a[MAXN];
struct Node{
    int id,L,R;Node(){}Node(int i,int l,int r):id(i),L(l),R(r){}
}dp[MAXN];
int surv[MAXN],Ans[MAXN];
bool FLB;
signed main(){
    cerr<<((&FLB)-(&FLA))/1024.0/1024.0<<endl;
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    rep(i,1,n){_a[i]=qread();}rep(i,1,m)c[i]=qread();
    int k=0;while((1<<k)<n)k++;
    rep(i,1,k){
        int e=1<<(k-i);
        string s;cin>>s;
        rep(j,1,e)d[i][j]=s[j-1]-'0';
    }
    int T;cin>>T;
    while(T--){
        int X[4];rep(i,0,3)X[i]=qread();
        rep(i,1,n){a[i]=_a[i]^X[i%4];}
        memset(Ans,0,sizeof(Ans));
        rep(k,0,20){
            if((1<<k)>n)break;
            rep(i,1,1<<k)surv[i]=i;
            rep(i,1,k){
                rep(j,1,(1<<k-i)){
                    if(d[i][j]){
                        surv[j]=(surv[2*j]>=i)?surv[2*j]:surv[2*j-1];
                    }else{surv[j]=(surv[2*j-1]>=i)?surv[2*j-1]:surv[2*j];}
                }
            }
            Ans[1<<k]=surv[1];
        }
        int ans=0;
        rep(i,1,m){ans^=(i*Ans[c[i]]);}
        cout<<ans<<endl;
    }
}

